package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ScrollView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityCommentsBinding;
import com.ihd.ihardware.pojo.CommentsRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.CommentsViewModel;
import com.ihd.ihardware.view.tzc.me.view.MyPageActivity;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;
import gallery.demo.com.gallery.model.GalleryPhotoModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CommentsActivity extends BaseActivity<ActivityCommentsBinding, CommentsViewModel> {

    private String signId;
    private SignsRes.DataBean.SignDetailsBean sdb;
    private Disposable msg0;
    private List<GalleryPhotoModel> urlList = new ArrayList<>();

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.keyboardEnable(true).statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("动态评论");
        binding.mtitlebar.setLeftBack(this);

        sdb = (SignsRes.DataBean.SignDetailsBean) getIntent().getSerializableExtra("signsBean");

        setRx(AppConstans.COMMENTS, new BaseConsumer<CommentsRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(CommentsRes mLiveData) {
                viewModel.setComments(mLiveData.getData().getSingComments());
                binding.commentNumsTV.setText(String.valueOf(mLiveData.getData().getSingComments().size()));
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        initData();

        binding.commentsRV.setNestedScrollingEnabled(false);
        binding.commentsRV.setAdapter(viewModel.mDTAdapter);
        signId = sdb.getSignId();
        viewModel.getComments(1, 100, signId);

        binding.sendIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.commentET.getText().length() > 0)
                    viewModel.sendComment(signId, binding.commentET.getText().toString());
                binding.commentET.setText("");
            }
        });

        binding.head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(), MyPageActivity.class);
                i.putExtra("userId", sdb.getUserId());
                v.getContext().startActivity(i);
            }
        });

        binding.taged.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sdb.isTaged())
                    viewModel.delTag(signId);
                else
                    viewModel.tag(signId);
            }
        });

        binding.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.photoGalleryView.showPhotoGallery(0, urlList, binding.bg);
            }
        });

        setRx(AppConstans.SENDCOMMENT, new BaseConsumer<CommentsRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(CommentsRes mLiveData) {
                DialogUtils.successDialog(CommentsActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "评论成功");
                viewModel.setComments(mLiveData.getData().getSingComments());
                binding.commentNumsTV.setText(String.valueOf(mLiveData.getData().getSingComments().size()));
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        binding.sv.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                });
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        msg0 = RxBus.getInstance().tObservable(AppConstans.TAG, Bundle.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle b) throws Exception {
                        if (b.getInt("key") == 0) {
                            if (b.getInt("type") == 0) {
                                sdb.setTagsNum(String.valueOf(Integer.parseInt(sdb.getTagsNum()) - 1));
                                sdb.setTaged(false);
                            } else {
                                sdb.setTagsNum(String.valueOf(Integer.parseInt(sdb.getTagsNum()) + 1));
                                sdb.setTaged(true);
                            }
                            initData();
                        } else
                            ToastUtils.showShort(CommentsActivity.this, b.getString("value"));
                    }
                });
    }

    private void initData() {
        if (!TextUtils.isEmpty(sdb.getAvatar()))
            Glide.with(CommentsActivity.this)
                    .load(Uri.parse(sdb.getAvatar()))
                    .apply(RequestOptions.circleCropTransform())
                    .into(binding.head);
        binding.nicknameTV.setText(sdb.getNickName());
        binding.dateTV.setText(sdb.getCreateTime());
        binding.zanTV.setText(sdb.getTagsNum());
        binding.textShowTV.setText(sdb.getTextShow());
        if (sdb.isTaged())
            binding.taged.setImageResource(R.drawable.icon_dianzan_selected);
        else
            binding.taged.setImageResource(R.drawable.icon_dianzan);
        Glide.with(CommentsActivity.this)
                .load(Uri.parse((sdb.getOssFiles() != null && sdb.getOssFiles().size() > 0) ? sdb.getOssFiles().get(0) : ""))
                .into(binding.bg);
        urlList.clear();
        urlList.add(new GalleryPhotoModel((sdb.getOssFiles() != null && sdb.getOssFiles().size() > 0) ? sdb.getOssFiles().get(0) : ""));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comments;
    }

    @Override
    protected Class getViewModelClass() {
        return CommentsViewModel.class;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (msg0 != null && !msg0.isDisposed())
            msg0.dispose();
    }
}
